-- Author: shunguo.chen
-- Date: 2016-06-17 11:42:45
--
--  MallController 商城／抽奖／充值 花钱的地方
--

local Mount = require("app.public.dao.Mount")

local MallView = import("..views.MallView")
local MallConfirmView = import("..views.MallConfirmView")
local LotteryResultView = import("..views.LotteryResultView")
local LotteryDropView = import("..views.LotteryDropView")
local ShopDrawResultView = import("..views.ShopDrawResultView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

local REFRESH_RUSH_INTERVAL = 1

function ClassRef:ctor(params)
	self.super.ctor(self, params)

end

function ClassRef:init()
	self._model = app:getInst("MallModel")

	self._refreshOpenCron = nil
	self._refreshRushCron = nil

	self._curIdx = 1
	self._curShopIdx = 1
	self._curLotteryIdx = 1

	self._isInLottery = false

	self._curShopDrawIdx = 1
	self._isInShopDraw = false

	self._isInOpenAmazingBox = false

	self._needCheckMidasPermission = false

	self._bagHandler = UD.bag:onChanged(handler(self, self.updateBag))
	self._playerHandler = UD.player:onChanged(handler(self, self.updatePlayer))
	self._badgeHandler = UD.badge:onChanged(handler(self, self.updateBadge))
	self._walletHandler = UD:onChanged("wallet", handler(self, self.updatePlayer))
end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
	UD.bag:unChanged(self._bagHandler)
	UD.player:unChanged(self._playerHandler)
	UD.badge:unChanged(self._badgeHandler)
	UD:unChanged("wallet", self._walletHandler)
end

function ClassRef:listEvent()
    return {
	    "onDailyUpdate",
	    "onMapChanged",
		"onFunctionClose",
		"OnAppEnterForeground",
		"onReconnectSuccessed",
		"OnAppEnterBackground",
		"onPayMPInfoUpdate",
	}
end


function ClassRef:onEvent(eventName, data)
	if eventName == "onDailyUpdate" then
		self._model:onDailyUpdate(self._curIdx, self._curShopIdx)
	elseif eventName == "onMapChanged" then
		if data then
			app.channelAPI:getPayMPInfo()
			self._model:reqLotteryInfos()
			self._model:reqPayDatas()
		end
	elseif eventName == "onFunctionClose" then
		-- 功能关闭，主要是服务器主动推送的关闭功能
		if self:isViewAlive() and table.hasvalue(Constant.SHOP_ORDER_LIST, data) then
			-- 如果有商城相关的功能关闭，更新相应ui
			if data == Constant.FUNC_TYPE_SHOP then
				self:closeView()
			else
				self._view:arrangeTabConfigs(false, data)
			end
		end
	elseif eventName == "OnAppEnterBackground" then
		self._needCheckMidasPermission = true
	elseif eventName == "OnAppEnterForeground" or eventName == "onReconnectSuccessed" then
		-- 从后台切回前台，网络好了后，检测一次权限
		if self._needCheckMidasPermission then
			self._model:reqCheckMidasPermission(true, function(msg)
				self._needCheckMidasPermission = false

				-- 如果在充值过程中，且在充值界面，还需要刷新充值界面
				if self._model:isInPay() and self:isViewAlive() then
					self._model:onCheckRecharge(msg)
				end
			end)
		end
	elseif eventName == "onPayMPInfoUpdate" then
		-- midas 营销活动信息更新
		self._model:onPayMPInfoUpdate(data)
	end
end

-- data = {tabKey= funcId, shopIndex=, itemId=, itemType=}
function ClassRef:openView(data)
	-- 服务器动态判定(任何一个页签打开都要先判断商场是否被服务器动态关闭)
    if UD:isFeatureClosedByServer(Constant.FUNC_TYPE_SHOP) then
		display.pushToast(L("tips_1110010"))
		return
	end	
	-- 剧情中不要打开商城界面
	if StoryManager:isEnterStoryMode() then return end

	local tabKey, shopIndex
	if data then
		tabKey = data.tabKey
		if tabKey == Constant.FUNC_TYPE_SHOPDRAW then
			self._curShopDrawIdx = self._model:getSelectedIdxById(data.shopIndex)
		else
			shopIndex = data.shopIndex
		end
		if data.itemType and data.itemId then
			self._itemInfo = {data.itemType, data.itemId}
		end
	end
	tabKey = tabKey or Constant.FUNC_TYPE_SHOP
	shopIndex = shopIndex or 1

	-- 功能是否开放
	if not UIHandler:isFunctionOpen(tabKey) then
		display.pushToast(L("tips_1110010"))
		return
	end

	local reqTimes = 0
	local function checkDataComp()
		reqTimes = reqTimes + 1
		if reqTimes == 2 then
			if cc.isDead(self._view) then
				self._view = MallView.new(app.mvc.VIEWTYPE_BLUR)
				self._view:onCloseView(handler(self, self.closeView))
				self._view:onClearView(handler(self, self.clearView))
				self._view:onOperate(handler(self, self.onOperate))
				self._view:onSwitchPanel(handler(self, self.onSwitchPanel))
			end	
			self:addChild(self._view, {notDelete=true})

			self._model:reqCheckMidasPermission()

			self._model:refreshAllMallNew()

			self._view:show(tabKey, shopIndex)
		end
	end
	
	self._model:reqGetShopDrawInfo(function()
		-- 奇迹卡片要判断是否有卡池开启(没开启要关闭页签，所以后续都要在请求完奇迹卡片数据之后再做)
		if tabKey == Constant.FUNC_TYPE_SHOPDRAW and not self._model:showDrawIsOpen() then
			display.pushToast(L("tips_1110010"))
			return
		end

		checkDataComp()
		self._model:reqMallDatas(checkDataComp)
		-- self._model:reqOpenServerGifts(checkDataComp)
		-- self._model:reqRefreshGiftPackage(checkDataComp)
	end)
end

function ClassRef:closeView(data)
	if self._view then
		self:removeChild(self._view, data)
	end
end
function ClassRef:clearView(data)
	if self._view then
		self._view = nil
	end

	self:_clearAllCron()
	
	self._curShopIdx = 1
	self._curLotteryIdx = 1
	self._curShopDrawIdx = 1

	self._model:stopMallNewCron()
	UD.badge:setNum(Constant.FUNC_TYPE_SHOP, 0)
end

-- 购买游戏资源
function ClassRef:buyGameRes(tid)
	local tabKey, shopIdx, itemType, itemId
	if tid == Constant.Lottery_ItemId or tid == Constant.ShopDraw_ItemId then
		-- 抽奖券
		tabKey = Constant.FUNC_TYPE_SHOP
		shopIdx = app:isInnerTencent() and 6 or 5
		itemType = Enums.lootType.item
		itemId = tid
	elseif tid == Enums.lootType.gold or
            tid == Enums.lootType.magicCrystal then

        -- 魔晶 和 金币
        tabKey = Constant.FUNC_TYPE_SHOP
        shopIdx = app:isInnerTencent() and 6 or 5
		itemType = tid
		itemId = 0
	elseif tid == Enums.lootType.coin then
		-- 钻石
		tabKey = Constant.FUNC_TYPE_RECHARGE
	end

	if not tabKey then return end
	
	-- 功能是否开放
	if not UIHandler:isFunctionOpen(tabKey) then
		display.pushToast(L("tips_1110010"))
		return
	end

	if tabKey == Constant.FUNC_TYPE_SHOP then
		if cc.isAlive(self._view) then
			self._itemInfo = {itemType, itemId}
			self._view:switchPanel(Constant.FUNC_TYPE_SHOP, shopIdx)
		else
			self:openView({
				tabKey = Constant.FUNC_TYPE_SHOP,
				shopIndex = shopIdx,
				itemType = itemType,
				itemId = itemId
			})
		end
	elseif tabKey == Constant.FUNC_TYPE_RECHARGE then
		if cc.isAlive(self._view) then
			self._view:switchPanel(Constant.FUNC_TYPE_RECHARGE)
		else
			self:openView({
				tabKey = Constant.FUNC_TYPE_RECHARGE,
			})
		end
	end
end

-- 在抽奖过程中
function ClassRef:isInLottery()
	return self._isInLottery
end

function ClassRef:setInLottery(b)
	self._isInLottery = b
end

function ClassRef:isInOpenAmazingBox()
	return self._isInOpenAmazingBox
end

function ClassRef:setInOpenAmazingBox(b)
	self._isInOpenAmazingBox = true
end

function ClassRef:openConfirmView(data)
	local view = MallConfirmView.new(nil, data)
	view:onCloseView(function(sender)
			UIHandler:removePopupUiByKey(sender:getViewName())
		end)
	UIHandler:addPopupUi(view, view:getViewName())
	view:show()
end

function ClassRef:openLotteryResultView(datas, type, param)
	local viewName = LotteryResultView:getViewName()
	local view = UIHandler:getSystemUiByKey(viewName)
	if cc.isDead(view) then
		app:sendMsg("UIController", "clearGetItemView")
		view = LotteryResultView.new(app.mvc.VIEWTYPE_BLUR)
		view:onCloseView(function(sender)
				self._isInLottery = false
				self._isInShopDraw = false
				self._isInOpenAmazingBox = false
				UIHandler:removeSystemUiByKey(viewName)
			end)
		view:onOperate(handler(self, self.onOperate))

		UIHandler:addSystemUi(view, viewName)
	end
	view:show()
	if type == "shopDraw" then
		view:refresh(nil, datas)
		view:updateMoneyInfo(param) -- param表示cardInfo
	elseif type == "amazingBox" then
		view:refresh(nil, datas)
		view:updateAmazingBoxMoneyInfo(param) --param表示moneyinfo
	else
		local info = self._model:getLotteryInfo(self._curLotteryIdx)
		view:refresh(info, datas)
		local coin = UD:getItemAmountByTid(Constant.Lottery_ItemId)
		view:updateCoin(coin)
	end
end
function ClassRef:closeLotteryResultView()
	local viewName = LotteryResultView:getViewName()
	UIHandler:removeSystemUiByKey(viewName)
end

function ClassRef:showNextLotteryResult()
	local view = UIHandler:getSystemUiByKey(LotteryResultView:getViewName())
	if view then
		view:showNext()
	end
end

function ClassRef:openNeedPayDialog(str)
--	display.pushToast(L("lua_code_text_297"))

	str = str or L("demondShop_notenough")
	local dlg = display.newConfirmDialog(str, function()
		self:buyGameRes(Enums.lootType.coin)
	end)
	UIHandler:addPopDialog(dlg)
end

function ClassRef:openNeedBuyDialog(str, id)
--	display.pushToast(L("lua_code_text_496"))

	str = str or L("lua_code_text_496")
	local dlg = display.newConfirmDialog(str, function()
		self:buyGameRes(id or Constant.Lottery_ItemId)
		self:closeLotteryResultView()
	end)
	UIHandler:addPopDialog(dlg)
end

-- 十连抽掉落预览界面
function ClassRef:openLotteryDropView(data, str)
	if not data or #data == 0 then return end
    local view = LotteryDropView.new()
    view:onCloseView(function(sender)
    	UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
    view:performWithDelay(function()
        view:createList(data, str)  
    end, 0)	  
end

-- 在翻牌抽奖过程中
function ClassRef:isInShopDraw()
	return self._isInShopDraw
end

function ClassRef:setInShopDraw(b)
	self._isInShopDraw = b
end

-- 翻牌抽奖奖励界面
function ClassRef:openShopDrawResultView(info)
	if not info then return end
	local view
	local cardInfo = info.resultInfo
	if cardInfo.type == 1 then
		-- 单抽
		app:sendMsg("UIController", "clearGetItemView")
		view = ShopDrawResultView.new()
		view:onCloseView(function(sender)
			self._isInShopDraw = false
			UIHandler:removePopupUiByKey(sender:getViewName())
		end)	
		UIHandler:addPopupUi(view, view:getViewName())	
		view:show()
		view:setData(info.lootData[1], cardInfo)
	elseif cardInfo.type == 2 then
		-- 十连抽
		self:openLotteryResultView(info.lootData, "shopDraw", cardInfo)
	end
end

function ClassRef:refreshShopDrawView(id)
	if cc.isDead(self._view) then return end
	self._curShopDrawIdx = self._model:getSelectedIdxById(id)
	self:refreshShopDraw()
end

-------------------------------------------------------------------
function ClassRef:onOperate(op, ...)
--	 print("MallController:onOperate", op, ...)
	local param, id = ...
	if op == "buyItem" then
		self._model:reqMallBuy(param, self._curShopIdx)
	elseif op == "switchMallShop" then
		self:refreshMall(param)
	elseif op == "preview" then
		EM:notify("avatarPreview", param)
	elseif op == "lottery" then
		self._model:reqLottery(self._curLotteryIdx, param)
	elseif op == "lotteryMount" then
		-- 抽到坐骑，需要弹框展示
		self:_lotteryMount(param)
	elseif op == "needPay" then
		-- 货币不足，是否前往充值
		self:openNeedPayDialog(param)
	elseif op == "needBuy" then
		-- 抽奖道具不足，是否前往购买
		self:openNeedBuyDialog(param, id)
	elseif op == "pay" then
		self._model:reqPay(param)
	elseif op == "openLotteryDropView" then
		local info, str = GD:getLotteryDropShowById(self._curLotteryIdx)
        self:openLotteryDropView(info, str)
	elseif op == "buyGift" then
		-- 购买小额礼包
        self._model:reqBuyGift(param)
	elseif op == "buyDailyGift" then	--海外版礼包购买，与购买钻石相同
		self._model:reqEmaRecharge(param)	--通知服务器加锁
--		self._model:reqPay(param)
	elseif op == "getFreeDailyGift" then 	--海外版礼包免费获得
		self:openFreeDailyGift(param)
	elseif op == "buyOpenServerGift" then		--购买开服礼包
		self._model:reqBuyOpenServerGift(param)
	elseif op == "switchShopDraw" then
		self._curShopDrawIdx = param
	elseif op == "shopDrawCard" then -- 翻牌抽奖系统
		self._model:reqDrawCard(param)
	elseif op == "openShopDrawDropView" then
		local info, str = GD:getShopDrawDropShowById(param)
		self:openLotteryDropView(info, str)
	end
end

function ClassRef:_lotteryMount(mount, breakItem)
	local tipStr
	if breakItem then
		-- 已经拥有此坐骑
		local itemNum = breakItem:getAmount()
		local localMount = UD:getMountByID(mount:getId())
		if localMount:isMaxQuality() then
			-- 此坐骑已经最高品质
			local itemName = breakItem:getName(true)
			tipStr = string.format(L("demondShop_mount_quility_max"), itemName, itemNum)
		else
			tipStr = string.format(L("demondShop_mount_own"), itemNum)
		end
	end

	app:sendMsg("MountController", "openNewView", mount, tipStr)
end

-- 局部刷新商城
function ClassRef:updateMall(datas, shopIdx)
	if not self:isViewAlive() then return end
	if  self._view:getCurPanelKey(self._curIdx) ~= Constant.FUNC_TYPE_SHOP then return end
	if self._curShopIdx ~= shopIdx then return end

	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_SHOP)
	if not panel then
		return
	end
	local renders = panel:getAllRenders()
	for _,v in ipairs(datas) do
		for _,render in ipairs(renders) do
			if render:getData() and render:getData().id == v.id then
				render:setData(v)
				break
			end
		end

		if panel:getData().id == v.id then
			panel:setData(v)
		end
	end
end

function ClassRef:updateMallNew()
	if not self:isViewAlive() then return end

	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_SHOP)
	if not panel then
		return
	end
	panel:refreshBtnsNew(self._model:getMallNews())
end

function ClassRef:updateLottery()
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_LOTTERY)
	if not panel then
		return
	end
	panel:refresh()
end

function ClassRef:updatePay(payData, keyOfPanel)
	if not self:isViewAlive() then return end
	
	local panel = self._view:getPanelByKey(keyOfPanel)
	if not panel then
		return
	end
	if panel.refreshDataByPayData then
		panel:refreshDataByPayData(payData)
	end
	-- local renders = panel:getRenders()
	-- for i,render in ipairs(renders) do
	-- 	local data = render:getData()
	-- 	if data and data.id == payData.id then
	-- 		render:refresh()
	-- 		break
	-- 	end
	-- end
end

function ClassRef:updateSuperGiftPackage(data, isGiftPackage)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_TENCENT_SUPER_GIFT)
	if not panel then return end
	panel:refreshPanel(data, isGiftPackage)
end

function ClassRef:updateGiftPackage(data, isGiftPackage)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_TENCENT_GIFT)
	if not panel then return end
	panel:refreshPanel(data, isGiftPackage)
end

function ClassRef:updateDailyGift(data, isGiftPackage)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_EMA_DAILY_GIFT)
	if not panel then return end
	panel:refreshPanel(data, isGiftPackage)
end

function ClassRef:updateDailyGiftEnabled(isEnabled)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_EMA_DAILY_GIFT)
	if not panel then return end
	panel:setGiftEnabled(isEnabled)
end

function ClassRef:updateOpenServerGifts(data, isRefreshSelect)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_EMA_SERVER_GIFT)
	if not panel then return end
	panel:refreshPanel(data, isRefreshSelect)

	local isAllSellOut = panel:getIsAllSellOut()
	if isAllSellOut then
		self._view:arrangeTabConfigs(false, Constant.FUNC_TYPE_EMA_SERVER_GIFT)
	end
end

------------------------------ view logic ---------------------------------
function ClassRef:updateBag(uid, item, amountGot, opt)
	if not self:isViewAlive() then return end
	if  self._view:getCurPanelKey(self._curIdx) ~= Constant.FUNC_TYPE_LOTTERY then return end

	if item:getId() == Constant.Lottery_ItemId then
        local coin = UD:getItemAmountByTid(Constant.Lottery_ItemId)
		self._view:updateCoin(coin)

        local viewName = LotteryResultView:getViewName()
        local view = UIHandler:getSystemUiByKey(viewName)
        if not cc.isDead(view) then
            view:updateCoin(coin)
        end
	end
end

function ClassRef:updatePlayer(name, old, new)
	if not self:isViewAlive() then return end
	if name == "coin" then
		if self._view:getCurPanelKey(self._curIdx) ~= Constant.FUNC_TYPE_LOTTERY then
			self._view:updateCoin(new)
		end
	elseif name == "gold" then
		
	elseif name == "magicCrystal" then
		if self._view:getCurPanelKey(self._curIdx) == Constant.FUNC_TYPE_SHOP then
			self._view:updateCrystal(new)
		end
	end
end

function ClassRef:updateBadge(id, num)
	if id == Constant.FUNC_TYPE_LOTTERY then
		if self:isViewAlive() then
			self._view:updateNew(num > 0)
		end
	end
end

--idx = 1 商城
--idx = 2 十连抽
--idx = 3 小额礼包
--idx = 4 充值
function ClassRef:onSwitchPanel(idx, shopIndex, tabKey)
--	print(idx, shopIndex)
	self._curIdx = idx
	if tabKey == Constant.FUNC_TYPE_SHOP then
		if shopIndex then
			self:setShopIndex(shopIndex)
		else
			self:refreshMall()
		end
	elseif tabKey == Constant.FUNC_TYPE_LOTTERY then
		self:refreshLottery()
	elseif tabKey == Constant.FUNC_TYPE_SHOPDRAW then
		self._model:reqGetShopDrawInfo()
	elseif tabKey == Constant.FUNC_TYPE_TENCENT_SUPER_GIFT then
		self._model:reqRefreshGiftPackage()
		-- self._model:reqGetGiftPackageInfo()
	elseif tabKey == Constant.FUNC_TYPE_TENCENT_GIFT then
		self._model:reqRefreshGiftPackage()
		-- self._model:reqGetGiftPackageInfo()
	elseif tabKey == Constant.FUNC_TYPE_RECHARGE then
		self._model:reqPayDatas(true)
		self:refreshPay()
	elseif tabKey == Constant.FUNC_TYPE_EMA_DAILY_GIFT then		--海外每日礼包
		self:updateDailyGift(self._model:getPayGifts(), true)
		self._model:reqPayDatas(true)
		self._model:reqGetGiftPackageInfo()
	elseif tabKey == Constant.FUNC_TYPE_EMA_SERVER_GIFT then	--海外开服礼包
		self._model:reqOpenServerGifts()
	end
	
	if tabKey == Constant.FUNC_TYPE_LOTTERY then
		self._view:updateCoin(UD:getItemAmountByTid(Constant.Lottery_ItemId))
	elseif tabKey == Constant.FUNC_TYPE_SHOPDRAW then
		self._view:updateMoneyInfo(self._curShopDrawIdx)
	else
		self._view:updateCoin(UD:getCoin())
		if tabKey == Constant.FUNC_TYPE_SHOP then
			self._view:updateCrystal(UD:getMagicCrystal())
		end
	end
end

function ClassRef:setShopIndex(shopIdx)
	local panel = self._view:getPanel(self._curIdx)
	if panel.setShopIndex then
		panel:setShopIndex(shopIdx)
	end
end

function ClassRef:refreshMall(shopIdx)
	self:_clearAllCron()

	local curInfo = nil
	local jumptoFlag = false -- 选中的物品是否顶格显示
	if shopIdx then
		self._curShopIdx = shopIdx
	end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_SHOP)
	if not panel then
		return
	end
	local curData = panel:getData()
	if shopIdx then
		curInfo = self._itemInfo
		self._itemInfo = nil
		jumptoFlag = true
	elseif curData then
		curInfo = curData.id
	end
	local datas, refreshGap = self._model:getMallDatas(self._curShopIdx)
	self:updateMallNew()

	-- print("refreshMall", #datas, refreshGap)

	local renderIdx = 1
	if curInfo then
		for i,data in ipairs(datas) do
			local tempType = protobuf.enum_id("pb_loot.Loot.LootType", data.itemType)
			if shopIdx and tempType == curInfo[1] and data.itemId == curInfo[2] then --商店
				renderIdx = i
				break
			elseif type(curInfo) == "number" and data.id == curInfo then
				renderIdx = i
				break
			end
		end
	end
	panel:refreshShop(datas, renderIdx, jumptoFlag)

	-- 数据有更新，定时的内容
	if refreshGap then
		self:_refreshOpen(refreshGap)
	end

	-- 刷新抢购
	self:_refreshRushBuy()
end

function ClassRef:refreshLottery()
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_LOTTERY)
	local info = self._model:getLotteryInfo(self._curLotteryIdx)
	panel:setData(info)
end

function ClassRef:refreshShopDraw()
	if cc.isDead(self._view) then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_SHOPDRAW)
	if not panel then return end
	panel:refreshShopDraw(self._curShopDrawIdx)
end

function ClassRef:refreshPay(isNeedReload)
	if not self:isViewAlive() then return end
	local panel = self._view:getPanelByKey(Constant.FUNC_TYPE_RECHARGE)
	if not panel then return end
	local payDatas = self._model:getPayDatas()
	if panel.reloadData then
		panel:reloadData(payDatas)
	end
end

function ClassRef:_clearAllCron()
	if self._refreshRushCron then
		scheduler.cancelCron(self._refreshRushCron)
		self._refreshRushCron = nil
	end
	if self._refreshOpenCron then
		scheduler.cancelCron(self._refreshOpenCron)
		self._refreshOpenCron = nil
	end
end

function ClassRef:_refreshOpen(gap)
	if self._refreshOpenCron then
		scheduler.cancelCron(self._refreshOpenCron)
		self._refreshOpenCron = nil
	end

	--每次刷新列表
	self._refreshOpenCron = scheduler.newCron(function()
			self._refreshOpenCron = nil
			self:refreshMall()
		end, gap)
end

function ClassRef:_refreshRushBuy()
	if self._refreshRushCron then
		scheduler.cancelCron(self._refreshRushCron)
		self._refreshRushCron = nil
	end

	if  self._view:getCurPanelKey(self._curIdx) ~= Constant.FUNC_TYPE_SHOP then return end
	local panel = self._view:getPanel(self._curIdx)
	if not panel then
		return
	end
	local datas = panel:getDatas()

	local rushParams = {}
	for i,data in ipairs(datas) do
		if data:isNeedRushRefresh() then
			table.insert(rushParams, {goodsId=data.id, svrLimitNowCount=data.svrLimitNowCount})
		end
	end

	-- 定时刷新全服抢购的数量，暂时不做
	if #rushParams > 0 then
		self._model:reqMallNum(self._curShopIdx, rushParams)
		self._refreshRushCron = scheduler.newCron(function()
				self._refreshRushCron = nil
				self:_refreshRushBuy()
			end, REFRESH_RUSH_INTERVAL)
	end
end

function ClassRef:openFreeDailyGift(param)
	if param.nowProgress >= param.needProgress then
		if param.isCanget then
			self._model:reqGetDailyGiftByTask()
		end
	else
		if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_DAILY_TASK) then
			app:sendMsg("DailyController", "openView")
			app:sendMsg("MallController", "closeView")
		else
			local feature = GD:queryFeatureOnById(Constant.FUNC_TYPE_DAILY_TASK)
			if feature then
				display.pushToast(string.format(L("openLvShow"), feature.level))
			end
		end
	end
end

return ClassRef
